package com.lilinling.yinzhi.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.lilinling.common.utils.JwtInfo;
import com.lilinling.common.utils.R;
import com.lilinling.yinzhi.order.feign.UserFeignService;
import com.lilinling.yinzhi.order.vo.OrderConfirmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.Query;

import com.lilinling.yinzhi.order.dao.OrderDao;
import com.lilinling.yinzhi.order.entity.OrderEntity;
import com.lilinling.yinzhi.order.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    UserFeignService userFeignService;

    @Autowired
    private OrderService orderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 下单
     * @param makerId
     * @param jwtInfo
     */
    @Override
    public Long submitOrder(Long makerId, JwtInfo jwtInfo) {
        //1、生成订单号
        String orderSn = IdWorker.getTimeId();
        OrderEntity entity = new OrderEntity();
        entity.setOrderSn(orderSn);
        //用户信息
        entity.setUserId(jwtInfo.getId());
        entity.setUserUsername(jwtInfo.getNickname());

        entity.setMakerId(makerId);

        entity.setStatus(0);//待付款
        entity.setAutoConfirmDay(7);
        entity.setDeleteStatus(0);//未删除
        entity.setCommentTime(new Date());
        entity.setModifyTime(new Date());
        this.baseMapper.insert(entity);

        return entity.getId();
    }

    @Override
    public PageUtils getUserOrderList(Map<String, Object> params, Long userId) {
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<OrderEntity>().eq("user_id",userId).orderByDesc("comment_time");
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

//    @Override
//    public List<OrderEntity> getUserOrderList(Long userId) {
//        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
//        wrapper.orderByDesc("create_time").last("limit 4");
//        List<OrderEntity> userOrderList = orderService.list(wrapper);
//
//    }

//    @Override
//    public OrderConfirmVo confirmOrder() {
//        OrderConfirmVo confirmVo = new OrderConfirmVo();
//
//        //1、远程查询制作人信息
//        userFeignService.getMakerOrderVoByMakerId();
//        return null;
//    }

}